﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace LabMS.Web.Equipment.LowValue
{
    public partial class lowvalueedit : Base.BasePage
    {
        public string strID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("DZPGL");//必须写在ispostback外
            strPageUserType.Add("mana");
            if (Request.QueryString["id"] == null)
            {
                Common.JShelper.JSAlert(Page, "err", "参数错误!");
                return;
            }
            strID = Request.QueryString["id"].ToString();

            if (!Page.IsPostBack)
            {
                ExtendModule.InstrumentAndConsumables instrument = checkQueryString(strID);
                if (instrument == null)
                {
                    Common.JShelper.JSAlert(Page, "err2", "字典类别不存在!");
                    return;
                }
                bindDDL(instrument);
                bind(instrument);

                tbProductionDate.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
                tbPurchaseDate.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");

                tbStateCode.Style.Add("display", "none");
                tbCountry.Attributes.Add("readOnly", "-1");
                tbPurchaseDate.Attributes.Add("readOnly", "-1");
                tbCreatedDate.Attributes.Add("readOnly", "-1");
                tbInstrumentNum.Attributes.Add("readOnly", "-1");
                tbInstrumentName.Attributes.Add("readOnly", "-1");
                tbKindNum.Attributes.Add("readOnly", "-1");
            }
        }

        ExtendModule.InstrumentAndConsumables checkQueryString(string strID)
        {
            ExtendBLL.InstrumentAndConsumables bconsum = new ExtendBLL.InstrumentAndConsumables();
            string strWhere = " Type = 'lowvalue' and ID =" + strID;
            List<ExtendModule.InstrumentAndConsumables> listinstrument = bconsum.GetExtendModelList(strWhere);
            if (listinstrument.Count <= 0)
            {
                return null;
            }
            return listinstrument[0];
        }

        public void bindDDL(ExtendModule.InstrumentAndConsumables instrument)
        {
            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();
            tbCreatedDate.Text = DateTime.Today.ToLongDateString();
            listCClass = eDictionary.GetItemListByDClass("haocaileibie");
            ddlCClass.DataSource = listCClass;
            ddlCClass.DataTextField = "Name";
            ddlCClass.DataValueField = "Code";
            ddlCClass.DataBind();

            listCClass = eDictionary.GetItemListByDClass("shebeilaiyuan");
            ddlSource.DataSource = listCClass;
            ddlSource.DataTextField = "Name";
            ddlSource.DataValueField = "Code";
            ddlSource.DataBind();

            listCClass = eDictionary.GetItemListByDClass("shiyongfangxiang");
            ddlArrow.DataSource = listCClass;
            ddlArrow.DataTextField = "Name";
            ddlArrow.DataValueField = "Code";
            ddlArrow.DataBind();

            listCClass = eDictionary.GetItemListByDClass("jingfeikemu");
            ddlFundsSubject.DataSource = listCClass;
            ddlFundsSubject.DataTextField = "Name";
            ddlFundsSubject.DataValueField = "Code";
            ddlFundsSubject.DataBind();

            listCClass = eDictionary.GetItemListByDClass("dangwei");
            ddlModle.DataSource = listCClass;
            ddlModle.DataTextField = "Name";
            ddlModle.DataValueField = "Code";
            ddlModle.DataBind();


            ExtendBLL.InstrumentAndConsumables eConsum = new ExtendBLL.InstrumentAndConsumables();
            int iMaxCID = eConsum.GetMaxId();
            MaxCID.Value = iMaxCID.ToString("D10");
            tbInstrumentNum.Text = iMaxCID.ToString("D10");

            tbLocation.Text = instrument.LabName;
            tbLocationCode.Text = instrument.StockLocation;

        }

        void bind(LabMS.Model.InstrumentAndConsumables instrument)
        {
            EquipPhoto.ImageUrl = "~/Equipment/PicShow.aspx?ID=" + instrument.ID;
            tbStandards.Text = instrument.Standards;
            tbContactPhone.Text = instrument.ContactPhone.ToString();
            tbResponesoblePerson.Text = instrument.ResponesoblePerson;
            tbPurpose.Text = instrument.Purpose;
            tbModel.Text = instrument.Model;
            tbKindNum.Text = instrument.ClassNumber.ToString();
            tbInstrumentName.Text = instrument.Name;
            tbInstrumentNum.Text = instrument.Numbers;
            tbCountry.Text = instrument.CountryCode;

            tbDocuments.Text = instrument.Documents.ToString();
            tbFactoryCode.Text = instrument.FactoryCode.ToString();
            tbAmount.Text = instrument.Quantity.ToString();
            tbUnitPrice.Text = instrument.UnitPrice;
            try
            {
                tbProductionDate.Text = instrument.ProductionDate.Value.ToShortDateString();
            }
            catch { }
            try
            {
                tbCreatedDate.Text = instrument.CreatedDate.Value.ToShortDateString();
            }
            catch { }
            try
            {
                tbPurchaseDate.Text = instrument.PurchaseDate.Value.ToShortDateString();
            }
            catch { }

            //tbVENDOR.Text = "";//应加上
        }

        protected void lbSave_Click(object sender, EventArgs e)
        {


            lbErr.Visible = false;
            Model.InstrumentAndConsumables instrument = new LabMS.Model.InstrumentAndConsumables();
            BLL.InstrumentAndConsumables binstrument = new LabMS.BLL.InstrumentAndConsumables();
            instrument.ID = Convert.ToInt64(strID);
            instrument.Arrow = ddlArrow.SelectedValue;
            instrument.ClassNumber = ddlCClass.SelectedValue;
            instrument.ClassNumber = tbKindNum.Text.Trim();
            instrument.CountryCode = tbCountry.Text;
            instrument.CreatedDate = DateTime.Today;
            instrument.Documents = tbDocuments.Text.Trim();
            instrument.FactoryCode = tbFactoryCode.Text.Trim();
            instrument.FundsSubject = ddlFundsSubject.SelectedValue;
            instrument.Measurement = ddlModle.SelectedValue;
            instrument.Model = tbModel.Text.Trim();
            instrument.Name = tbInstrumentName.Text.Trim();
            instrument.Numbers = tbInstrumentNum.Text.Trim();
            instrument.OperatorDate = DateTime.Now;
            DateTime tempTime = new DateTime();
            if (DateTime.TryParse(tbProductionDate.Text.Trim(), out tempTime))
            {
                instrument.ProductionDate = tempTime;
            }
            if (DateTime.TryParse(tbPurchaseDate.Text.Trim(), out tempTime))
            {
                instrument.PurchaseDate = tempTime;
            }
            decimal iTemp = 0;
            if (decimal.TryParse(tbAmount.Text.Trim(), out iTemp))
            {
                instrument.Quantity = iTemp;
            }
            instrument.Source = ddlSource.SelectedValue;
            instrument.StockLocation = tbLocationCode.Text;
            instrument.PresentSituation = ddlPresentSituation.SelectedValue;
            instrument.Type = "lowvalue";
            instrument.ContactPhone = tbContactPhone.Text;
            instrument.ResponesoblePerson = tbResponesoblePerson.Text;
            instrument.UnitPrice = tbUnitPrice.Text.Trim();
            instrument.Purpose = tbPurpose.Text;
            instrument.Standards = tbStandards.Text;
            //修改图片
            try
            {
                if (imageUpload.HasFile)
                {
                    HttpPostedFile upPhoto = imageUpload.PostedFile;
                    int upPhotoLength = upPhoto.ContentLength;
                    string PhotoContentType = upPhoto.ContentType;
                    byte[] PhotoArray = new byte[upPhotoLength];
                    Stream PhotoStream = upPhoto.InputStream;
                    PhotoStream.Read(PhotoArray, 0, upPhotoLength);

                    instrument.Pic = PhotoArray;//相片
                    instrument.PicType = PhotoContentType;//图片类型
                }
            }
            catch
            {
                LabMS.Common.JShelper.JSAlert(Page, "Picwrong", "程序错误:图片保存出错");
            }
            //无供应商
            try
            {
                binstrument.Update(instrument);
                LabMS.Common.JShelper.JSAlertAndRedirect(Page, "AddSuccess", "修改成功!", "lowvalueedit.aspx?ID=" + strID);
            }
            catch (Exception ex)
            {
                lbErr.Visible = true;
                lbErr.Text = ex.Message.ToString();
            }

        }
    }
}